import { MockMethod } from 'vite-plugin-mock'

let users = [
  {
    id: 1,
    username: 'admin',
    nickname: '管理员',
    email: 'admin@example.com',
    phone: '13800138000',
    role: '超级管理员',
    status: '启用',
    createTime: '2024-01-01 12:00:00'
  },
  {
    id: 2,
    username: 'user',
    nickname: '普通用户',
    email: 'user@example.com',
    phone: '13800138001',
    role: '普通用户',
    status: '启用',
    createTime: '2024-01-02 12:00:00'
  },
  {
    id: 3,
    username: 'anbang',
    nickname: '超级管理员',
    email: 'anbang@example.com',
    phone: '13800138002',
    role: '超级管理员',
    status: '启用',
    createTime: '2024-01-02 12:00:00'
  }
]

let roles = [
  {
    id: 1,
    name: '超级管理员',
    description: '系统最高权限',
    permissions: [],
    createTime: '2024-01-01 12:00:00'
  },
  {
    id: 2,
    name: '普通用户',
    description: '基础权限',
    permissions: [],
    createTime: '2024-01-02 12:00:00'
  }
]

const tokens = {
  admin: {
    username: 'admin',
    roles: ['admin'],
    accessToken: 'admin-token'
  },
  user: {
    username: 'user',
    roles: ['user'],
    accessToken: 'user-token'
  },
  anbang: {
    username: 'anbang',
    roles: ['anbang'],
    accessToken: 'user-token'
  }
}

const permissions = [
  {
    id: '1',
    name: '系统管理',
    code: 'system',
    children: [
      {
        id: '1-1',
        name: '用户管理',
        code: 'system:user',
        children: [
          { id: '1-1-1', name: '查看用户', code: 'system:user:view' },
          { id: '1-1-2', name: '添加用户', code: 'system:user:add' },
          { id: '1-1-3', name: '编辑用户', code: 'system:user:edit' },
          { id: '1-1-4', name: '删除用户', code: 'system:user:delete' }
        ]
      },
      {
        id: '1-2',
        name: '角色管理',
        code: 'system:role',
        children: [
          { id: '1-2-1', name: '查看角色', code: 'system:role:view' },
          { id: '1-2-2', name: '添加角色', code: 'system:role:add' },
          { id: '1-2-3', name: '编辑角色', code: 'system:role:edit' },
          { id: '1-2-4', name: '删除角色', code: 'system:role:delete' }
        ]
      }
    ]
  },
  {
    id: '2',
    name: '商品管理',
    code: 'product',
    children: [
      { id: '2-1', name: '查看商品', code: 'product:view' },
      { id: '2-2', name: '添加商品', code: 'product:add' },
      { id: '2-3', name: '编辑商品', code: 'product:edit' },
      { id: '2-4', name: '删除商品', code: 'product:delete' }
    ]
  }
]

export default [
  {
    url: '/api/login',
    method: 'post',
    response: ({ body }) => {
      const { username, password } = body
      if (username === 'admin' && password === '123456') {
        return {
          code: 200,
          message: '登录成功',
          data: {
            accessToken: 'admin-token',
            username: 'admin',
            roles: ['admin'],
            permissions: [
              'route:view',
              'route:add',
              'route:edit',
              'route:delete',
              'user:view',
              'user:add',
              'user:edit',
              'user:delete',
              'user:permission',
              'permission:view',
              'permission:add',
              'permission:edit',
              'permission:delete'
            ]
          }
        }
      }
      return {
        code: 401,
        message: '用户名或密码错误'
      }
    }
  },
  {
    url: '/api/userInfo',
    method: 'get',
    response: ({ headers }) => {
      const token = headers.authorization?.replace('Bearer ', '')
      if (token === 'admin-token') {
        return {
          code: 200,
          data: tokens.admin,
          message: 'success'
        }
      }
      if (token === 'user-token') {
        return {
          code: 200,
          data: tokens.user,
          message: 'success'
        }
      }
      return {
        code: 401,
        message: '无效的token'
      }
    }
  },
  {
    url: '/api/users',
    method: 'get',
    response: () => ({
      code: 200,
      data: users,
      message: 'success'
    })
  },
  {
    url: '/api/user',
    method: 'post',
    response: ({ body }) => {
      const newUser = {
        ...body,
        id: users.length + 1,
        createTime: new Date().toLocaleString()
      }
      users.push(newUser)
      return {
        code: 200,
        data: newUser,
        message: 'success'
      }
    }
  },
  {
    url: '/api/user/:id',
    method: 'put',
    response: ({ body }) => {
      const index = users.findIndex(item => item.id === body.id)
      if (index > -1) {
        users[index] = { ...users[index], ...body }
        return {
          code: 200,
          data: users[index],
          message: 'success'
        }
      }
      return {
        code: 400,
        message: '用户不存在'
      }
    }
  },
  {
    url: '/api/user/:id',
    method: 'delete',
    response: ({ params }) => {
      const index = users.findIndex(item => item.id === Number(params.id))
      if (index > -1) {
        users.splice(index, 1)
        return {
          code: 200,
          message: 'success'
        }
      }
      return {
        code: 400,
        message: '用户不存在'
      }
    }
  },
  {
    url: '/api/roles',
    method: 'get',
    response: () => ({
      code: 200,
      data: roles,
      message: 'success'
    })
  },
  {
    url: '/api/role',
    method: 'post',
    response: ({ body }) => {
      const newRole = {
        ...body,
        id: roles.length + 1,
        createTime: new Date().toLocaleString()
      }
      roles.push(newRole)
      return {
        code: 200,
        data: newRole,
        message: 'success'
      }
    }
  },
  {
    url: '/api/role/:id',
    method: 'put',
    response: ({ body }) => {
      const index = roles.findIndex(item => item.id === body.id)
      if (index > -1) {
        roles[index] = { ...roles[index], ...body }
        return {
          code: 200,
          data: roles[index],
          message: 'success'
        }
      }
      return {
        code: 400,
        message: '角色不存在'
      }
    }
  },
  {
    url: '/api/role/:id',
    method: 'delete',
    response: ({ params }) => {
      const index = roles.findIndex(item => item.id === Number(params.id))
      if (index > -1) {
        roles.splice(index, 1)
        return {
          code: 200,
          message: 'success'
        }
      }
      return {
        code: 400,
        message: '角色不存在'
      }
    }
  },
  {
    url: '/api/permissions',
    method: 'get',
    response: () => ({
      code: 200,
      data: permissions,
      message: 'success'
    })
  }
] as MockMethod[]